#include "billprinterapp.h"

BillPrinterApp::BillPrinterApp(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle("票据套打V1.0");
    resize(800, 600);
    
    initUI();
}

BillPrinterApp::~BillPrinterApp()
{
}

void BillPrinterApp::initUI()
{
    // 创建中央窗口部件
    QWidget* centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);
    
    // 创建主布局
    QVBoxLayout* mainLayout = new QVBoxLayout(centralWidget);
    
    // 模板管理区域
    QHBoxLayout* templateLayout = new QHBoxLayout();
    templateLayout->addWidget(new QLabel("当前模板:"));
    templateLabel = new QLabel(templateObj.templateName);
    templateLayout->addWidget(templateLabel);
    
    QPushButton* newTemplateButton = new QPushButton("新建模板");
    QPushButton* loadTemplateButton = new QPushButton("加载模板");
    QPushButton* saveTemplateButton = new QPushButton("保存模板");
    
    connect(newTemplateButton, &QPushButton::clicked, this, &BillPrinterApp::newTemplate);
    connect(loadTemplateButton, &QPushButton::clicked, this, &BillPrinterApp::loadTemplate);
    connect(saveTemplateButton, &QPushButton::clicked, this, &BillPrinterApp::saveTemplate);
    
    templateLayout->addWidget(newTemplateButton);
    templateLayout->addWidget(loadTemplateButton);
    templateLayout->addWidget(saveTemplateButton);
    
    mainLayout->addLayout(templateLayout);
    
    // 数据输入区域
    dataInputWidget = new QWidget();
    dataInputLayout = new QVBoxLayout(dataInputWidget);
    
    // 初始化数据输入区域
    updateDataInputs();
    
    mainLayout->addWidget(dataInputWidget);
    
    // 打印按钮区域
    QHBoxLayout* buttonLayout = new QHBoxLayout();
    
    QPushButton* previewButton = new QPushButton("打印预览");
    QPushButton* printButton = new QPushButton("直接打印");
    
    connect(previewButton, &QPushButton::clicked, this, &BillPrinterApp::previewPrint);
    connect(printButton, &QPushButton::clicked, this, &BillPrinterApp::directPrint);
    
    buttonLayout->addWidget(previewButton);
    buttonLayout->addWidget(printButton);
    
    mainLayout->addLayout(buttonLayout);
}

void BillPrinterApp::newTemplate()
{
    TemplateEditorDialog dialog(this);
    if (dialog.exec() == QDialog::Accepted) {
        templateObj = dialog.getTemplate();
        templateLabel->setText(templateObj.templateName);
        dataValues.clear();  // 清空数据
        updateDataInputs();
    }
}

void BillPrinterApp::loadTemplate()
{
    QString filename = QFileDialog::getOpenFileName(
        this, "加载模板", "", "模板文件 (*.json);;所有文件 (*)"
    );
    
    if (!filename.isEmpty()) {
        templateObj = BillTemplate::loadFromFile(filename);
        templateLabel->setText(templateObj.templateName);
        dataValues.clear();  // 清空数据
        updateDataInputs();
    }
}

void BillPrinterApp::saveTemplate()
{
    if (templateObj.fields.isEmpty()) {
        QMessageBox::warning(this, "警告", "模板为空，无法保存");
        return;
    }
    
    QString filename = QFileDialog::getSaveFileName(
        this, "保存模板", templateObj.templateName + ".json", "模板文件 (*.json);;所有文件 (*)"
    );
    
    if (!filename.isEmpty()) {
        if (templateObj.saveToFile(filename)) {
            QMessageBox::information(this, "成功", "模板保存成功");
        } else {
            QMessageBox::critical(this, "错误", "模板保存失败");
        }
    }
}

void BillPrinterApp::updateDataInputs()
{
    // 清除现有输入
    while (dataInputLayout->count()) {
        QLayoutItem* item = dataInputLayout->takeAt(0);
        if (item->widget()) {
            delete item->widget();
        }
        delete item;
    }
    
    // 为每个字段创建输入框
    for (const auto& field : templateObj.fields) {
        QString fieldName = field.name;
        QHBoxLayout* inputLayout = new QHBoxLayout();
        inputLayout->addWidget(new QLabel(fieldName + ":"));
        
        QLineEdit* inputField = new QLineEdit(dataValues.value(fieldName, ""));
        connect(inputField, &QLineEdit::textChanged, [this, fieldName](const QString& text) {
            dataValues[fieldName] = text;
        });
        
        inputLayout->addWidget(inputField);
        dataInputLayout->addLayout(inputLayout);
    }
}

void BillPrinterApp::previewPrint()
{
    QPrinter printer(QPrinter::HighResolution);
    QPrintPreviewDialog preview(&printer, this);
    connect(&preview, &QPrintPreviewDialog::paintRequested, this, &BillPrinterApp::printBill);
    preview.exec();
}

void BillPrinterApp::directPrint()
{
    QPrinter printer(QPrinter::HighResolution);
    QPrintDialog dialog(&printer, this);
    if (dialog.exec() == QDialog::Accepted) {
        printBill(&printer);
    }
}

void BillPrinterApp::printBill(QPrinter* printer)
{
    QPainter painter;
    if (!painter.begin(printer)) {
        return;
    }
    
    // 设置字体和画笔
    painter.setPen(Qt::black);
    
    // 打印每个字段
    for (const auto& field : templateObj.fields) {
        QString fieldName = field.name;
        QString fieldValue = dataValues.value(fieldName, "");
        
        // 转换毫米到像素
        float x = field.x * printer->logicalDpiX() / 25.4;
        float y = field.y * printer->logicalDpiY() / 25.4;
        float width = field.width * printer->logicalDpiX() / 25.4;
        float height = field.height * printer->logicalDpiY() / 25.4;
        
        // 设置字体大小
        QFont font = painter.font();
        font.setPointSizeF(field.fontSize);
        painter.setFont(font);
        
        // 绘制文本
        painter.drawText(QRectF(x, y, width, height), Qt::AlignLeft | Qt::AlignVCenter, fieldValue);
    }
    
    painter.end();
}
